package com.maoye.ossp.biz.statement.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.biz.statement.entity.StatementReceiveDetail;
import com.maoye.ossp.biz.statement.entity.StatementReceiveNote;
import com.maoye.ossp.biz.statement.service.StatementReceiveDetailService;
import com.maoye.ossp.biz.statement.service.StatementReceiveNoteService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
/**
 * 验收单controller类
 * @author yanghao
 *
 */
@Controller
@RequestMapping(value = "biz/statement/statementReceiveNote/")
public class StatementReceiveNoteController extends BaseController {
	
	@Resource
	private StatementReceiveNoteService statementReceiverNoteService;
	@Resource
	StatementReceiveDetailService statementReceiveDetailService;
	
	@Resource
	private ISupplierService supplierService;
	/**
	 * 列表展示验收单
	 * @param statementReceiveNote
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author yangh
	 * @data 2014年4月30日
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "list")
	public String list(StatementReceiveNote statementReceiveNote, HttpServletRequest request,
			HttpServletResponse response, Model model){
		       //获取登陆用户Id 
				Long userId=UserUtils.getCurrentUserId();
				//根据当前用户Id 如果是供应商 获取用户获取供应商信息
				Supplier supplier=supplierService.get(userId);
				QueryFilter queryFilter = getQuerFilter(request, statementReceiveNote);	
				//设置供应商编码
				if(supplier!=null){
					queryFilter.addFilter("supplierNo", supplier.getSupplierNo(), QueryOP.EQUAL);
				 }
		queryFilter.addFilter("billType", statementReceiveNote.getBillType(), QueryOP.EQUAL);
		queryFilter.addFilter("receiptDate", statementReceiveNote.getReceiptDate(), QueryOP.EQUAL);
		Page<StatementReceiveNote> page = (Page<StatementReceiveNote>) statementReceiverNoteService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/statement/statementReceive/list", model, request);
	}
	/**
	 * 验收费详细展示
	 * @param id
	 * @param model
	 * @return
	 * @author yangh
	 * @data 2014年4月30日
	 */
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id,StatementReceiveDetail statementReceiveDetail,
			HttpServletResponse response, Model model,HttpServletRequest request){
		StatementReceiveNote receiveNote = statementReceiverNoteService.get(id);
		model.addAttribute("receiveNote", receiveNote);
		
		String billNo = request.getParameter("billNo");
		QueryFilter queryFilter = getQuerFilter(request,statementReceiveDetail);
		queryFilter.getParams().put("billNo",billNo);
		Page<StatementReceiveDetail> page = (Page<StatementReceiveDetail>) statementReceiveDetailService.find(queryFilter);
		
		
		StatementReceiveDetail  varStatementReceiveDetaill=new StatementReceiveDetail();
		String total="合计";
		double quantity=0;
		double totalMoney=0;
		for(StatementReceiveDetail receiveDetail:page.getList()){
			 quantity=quantity+receiveDetail.getQuantity();
			 totalMoney=totalMoney+receiveDetail.getAmount();
		}
		varStatementReceiveDetaill.setMantr(total);
		varStatementReceiveDetaill.setQuantity(quantity);
		varStatementReceiveDetaill.setAmount(totalMoney);
		page.getList().add(varStatementReceiveDetaill);
		model.addAttribute("listSize",page.getList().size());
		model.addAttribute("page", page);
		return "/biz/statement/statementReceive/view";
	}
	/**
	 * 查询验收单明细
	 * @param statementReceiveNote
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author yangh
	 * @data 2014年4月30日
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "getByBillNo")
	public String getByBillNo(StatementReceiveDetail statementReceiveDetail ,HttpServletRequest request,
			HttpServletResponse response, Model model){
		QueryFilter queryFilter = getQuerFilter(request,statementReceiveDetail);
		queryFilter.addFilter("billNo", statementReceiveDetail.getBillNo(), QueryOP.EQUAL);
		Page<StatementReceiveDetail> page = (Page<StatementReceiveDetail>) statementReceiveDetailService.find(queryFilter);
		model.addAttribute("page", page);
		return "/biz/statement/statementReceive/detailview";
	}
	
	
	

	/**
	 * 导出验收单详细信息
	 * @param statementOrder
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "ExportDetailToExcel")
	public void ExportDetailToExcel(StatementReceiveNote statementReceiveNote,StatementReceiveDetail statementReceiveDetail  , HttpServletRequest request,
			HttpServletResponse response) {
		String id=request.getParameter("id");
		StatementReceiveNote receiveNote = statementReceiverNoteService.get(id);
		if(receiveNote.getBillType().equals("101")){
			receiveNote.setBillType("收货");
		}else if(receiveNote.getBillType().equals("102")){
			receiveNote.setBillType("收货冲销");
		}else if(receiveNote.getBillType().equals("161")){
			receiveNote.setBillType("退货");
		}else if(receiveNote.getBillType().equals("162")){
			receiveNote.setBillType("冲销");
		}else{
			receiveNote.setBillType("不存在");
		}
		
		String billNo = request.getParameter("billNo");
		QueryFilter queryFilter = getQuerFilter(request,statementReceiveDetail);
		queryFilter.getParams().put("billNo",billNo);
		Page<StatementReceiveDetail> page = (Page<StatementReceiveDetail>) statementReceiveDetailService.find(queryFilter);
		
		StatementReceiveDetail  varStatementReceiveDetaill=new StatementReceiveDetail();
		String total="合计";
		double quantity=0;
		double totalMoney=0;
		for(StatementReceiveDetail receiveDetail:page.getList()){
			 quantity=quantity+receiveDetail.getQuantity();
			 totalMoney=totalMoney+receiveDetail.getAmount();
		}
		varStatementReceiveDetaill.setMantr(total);
		varStatementReceiveDetaill.setQuantity(quantity);
		varStatementReceiveDetaill.setAmount(totalMoney);
		page.getList().add(varStatementReceiveDetaill);
		try {
			// 设置输出的格式
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			//获取浏览器类型
			String user_agent = request.getHeader("User-Agent").toLowerCase();
			String fileName="";
			//为不同的浏览器，对文件名进行不同的编码转换
			if(user_agent.indexOf("firefox") > 0){
				 fileName =  new String("验收单明细".getBytes("UTF-8"), "iso8859-1");
			}else{
				fileName = URLEncoder.encode("验收单明细", "UTF-8");
			}
			response.setHeader("Content-disposition","attachment; filename="+fileName+".xls");
			OutputStream os = response.getOutputStream();
			statementReceiverNoteService.ExportDetailToExcel(receiveNote, page.getList(), os);
			response.flushBuffer();
		} catch(IOException ex){
			System.out.println("导出数据出错");
		} catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	
	/**
	 * 验收费详细展示
	 * @param id
	 * @param model
	 * @return
	 * @author yangh
	 * @data 2014年4月30日
	 */
	@RequestMapping(value = "print")
	public String print(StatementReceiveNote statementReceiveNote,StatementReceiveDetail statementReceiveDetail,
			HttpServletResponse response, Model model,HttpServletRequest request){
		 
		String id=request.getParameter("id");
		StatementReceiveNote receiveNote = statementReceiverNoteService.get(id);
		
		String billNo = request.getParameter("billNo");
		QueryFilter queryFilter = getQuerFilter(request,statementReceiveDetail);
		queryFilter.getParams().put("billNo",billNo);
		Page<StatementReceiveDetail> page = (Page<StatementReceiveDetail>) statementReceiveDetailService.find(queryFilter);
		
		
		StatementReceiveDetail  varStatementReceiveDetaill=new StatementReceiveDetail();
		String total="合计";
		double quantity=0;
		double totalMoney=0;
		for(StatementReceiveDetail receiveDetail:page.getList()){
			 quantity=quantity+receiveDetail.getQuantity();
			 totalMoney=totalMoney+receiveDetail.getAmount();
		}
		varStatementReceiveDetaill.setMantr(total);
		varStatementReceiveDetaill.setQuantity(quantity);
		varStatementReceiveDetaill.setAmount(totalMoney);
		page.getList().add(varStatementReceiveDetaill);
		model.addAttribute("listSize",page.getList().size());
		model.addAttribute("receiveNote", receiveNote);
		model.addAttribute("list", page.getList());
		return "/biz/statement/statementReceive/print";
	}
	
	
	public StatementReceiveNoteService getStatementReceiverNoteService() {
		return statementReceiverNoteService;
	}

	public void setStatementReceiverNoteService(
			StatementReceiveNoteService statementReceiverNoteService) {
		this.statementReceiverNoteService = statementReceiverNoteService;
	}
}
